package com.lxp.common.model;

import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ToString
public class SystemContext {
    public static final Logger logger = LoggerFactory.getLogger(SystemContext.class);

    public String labelerHost1 = "192.168.1.85";
    public Integer labelerPort1 = 9100;
    public Integer labelerCount1 = 0;

    public String labelerHost2 = "192.168.1.85";
    public Integer labelerPort2 = 9200;
    public Integer labelerCount2 = 0;


    public Integer cameraPort = 9300;
    public Integer cameraCount = 11;

    public String plcHost = "192.168.1.85";
    public Integer plcPort = 9600;
    //public Integer plcErrPort = 4500;

    public String plcErrZAxis = "025";
    public String plcErrYAxis = "040";

    private SystemContext() {
    }
/*
    public static void loadTemplate() {
        try {
            Application.map.clear();
            List<FnTemplate> list = Db.use().query(Sql.getTemplateList, FnTemplate.class);
            for (FnTemplate template : list) {
                Application.map.put(template.getId().toString(), template);
            }
            logger.info("加载数据库模板成功！条数: " + list.size());
        } catch (Exception e) {
            logger.error("加载数据库模板失败！", e);
            JOptionPane.showMessageDialog(MainFrame.getInstance(), "数据库模板加载失败!", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static SystemContext loadContext() {
        try {
            byte[] bytes = Files.readAllBytes(Paths.get(configSettingsFile));
            Application.context = JSON.parseObject(bytes, SystemContext.class);
            logger.info("CameraContext:\r\n{}", toJsonFormat());
            logger.info("加载{}运行时配置文件成功！", configSettingsFile);
            return Application.context;
        } catch (IOException e) {
            logger.error("加载运行时配置文件失败！[{}]", configSettingsFile);
        }
        return new SystemContext();
    }

    public static void saveContext(boolean flag) {
        try {
            Path path = Paths.get(configSettingsFile);
            Files.write(path, toJsonFormat().getBytes(StandardCharsets.UTF_8));
            logger.info("保存的CameraContext:\r\n{}", toJsonFormat());
            logger.info("保存{}配置文件成功！", configSettingsFile);
            if (flag) {
                JOptionPane.showMessageDialog(ConfigFrame.getInstance(), "保存系统配置成功!\r\n" + configSettingsFile, "保存系统配置", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (IOException e) {
            logger.error("保存系统配置错误！{}", e.getMessage());
            if (flag) {
                JOptionPane.showMessageDialog(ConfigFrame.getInstance(), "保存系统配置错误!\r\n" + configSettingsFile, "保存系统配置", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static String toJsonFormat() {
        return JSON.toJSONString(Application.context, true);
    }*/
}
